#!/bin/bash

source /AIR_FLASH/script/air.conf
source /AIR_FLASH/script/shcommon

src=$1
##dst="md5tmp"
###dst=`echo ${VER_NAME%.tar*}`
dst=newsoft
function return_fail()
{
     rm -fr ${upgrade_DIR}/${src} > /dev/null 2>&1
     rm -fr ${upgrade_DIR}/${dst} > /dev/null 2>&1
     Error "fail in md5_checker.sh"
     exit 0
}
if [ ! -f ${upgrade_DIR}/${src} ] ; then
    Error "${upgrade_DIR}/${src} not file in md5_checker.sh"
    return_fail
fi 

chmod 777 ${upgrade_DIR}/${src} > /dev/null 2>&1

mkdir -p ${upgrade_DIR}/${dst}	> /dev/null 2>&1
tar -xvf ${upgrade_DIR}/${src} -C ${upgrade_DIR}/${dst} > /dev/null 2>&1


if [ ! -f "${upgrade_DIR}/${dst}/md5" ] ; then
     Error "${upgrade_DIR}/${dst}/md5 not file in md5_checker.sh"
     return_fail
fi

md5=`cat ${upgrade_DIR}/${dst}/md5`

if [ -z ${md5} ] ; then 
    Error "md5 not empty in md5_checker.sh"
    return_fail
fi

ret=`md5sum ${upgrade_DIR}/${dst}/AIRFLASH.tar.gz | grep ${md5} | wc -c`

if [[ ! $ret ]] || [[ $ret -lt 32 ]] ; then
    Error "${upgrade_DIR}/${dst}/AIRFLASH.tar.gz != ${md5} $ret in md5_checker.sh"
    return_fail
fi

cd ${upgrade_DIR}/${dst}
tar -xvf AIRFLASH.tar.gz  > /dev/null 2>&1

rm -fr ${upgrade_DIR}/backup/  > /dev/null 2>&1
rm -fr ${upgrade_DIR}/elder/  > /dev/null 2>&1

mkdir -p ${upgrade_DIR}/backup
cp ${prefix}/$APP_NAME ${prefix}/taubr.sh ${prefix}/script -r ${upgrade_DIR}/backup/  > /dev/null 2>&1

#rm -fr ${upgrade_DIR}/${dst} > /dev/null 2>&1
#echo ${md5}
echo "ok"
 
